<template>
	<view>
		<!-- <u-navbar :is-back="true" :is-fixed="true" title="基础信息"></u-navbar> -->
		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 50px);" :show-scrollbar="false">
			<view class="pb-15">
				<free-divider></free-divider>
				<free-list-item title="营业执照" :showRight="false" :showLeftIcon="false" @click="uploadBusinessLicense()">
					<u-image slot="right" :src="user.fullAvatar" style="width: 100px; height: 80px;"
						@error="handleImageError" mode="widthFix"></u-image>
				</free-list-item>
				<u-field :disabled="!edit" v-model="user.enterpriseName" label="企业名称" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-field v-model="user.phone" v-if="!edit" label="手机号" :disabled="true" input-align="right"></u-field>

				<u-field :disabled="!edit" v-model="user.unifiedCreditCode" label="统一社会信用代码" label-width="230"
					input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="user.registeredCapital" label="注册资本" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="user.type" label="类型" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="!edit" v-model="user.legalRepresentative" label="法定代表人" label-width="180"
					input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>

				<u-field :disabled="true" :right-icon="edit?'arrow-down-fill':''" @click="showFoundedTimeHandler"
					v-model="user.foundedTime" label="成立日期" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-picker mode="time" @confirm="selectFoundedTimeTerm" :default-time="user.foundedTime"
					v-model="foundedTimeShow" :params="params"></u-picker>

				<template v-if="!edit">
					<u-field :disabled="true" v-model="user.businessTerm" label="营业期限" input-align="right"
						:field-style="edit?placeholderStyle:{}"></u-field>
				</template>
				<template v-else>
					<u-field :disabled="true" @click="showBusinessStartTermHandler"
						:right-icon="edit?'arrow-down-fill':''" v-model="user.businessStartTerm" label="营业期限开始时间"
						input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>
					<u-picker mode="time" @confirm="selectBusinessStartTerm" :default-time="user.businessStartTerm"
						v-model="businessStartTermShow" :params="params"></u-picker>

					<u-field :disabled="true" @click="showBusinessEndTermHandler"
						:right-icon="edit?'arrow-down-fill':''" v-model="user.businessEndTerm" label="营业期限结束时间"
						input-align="right" :field-style="edit?placeholderStyle:{}"></u-field>
					<u-picker mode="time" @confirm="selectBusinessEndTerm" :default-time="user.businessEndTerm"
						v-model="businessEndTermShow" :params="params"></u-picker>
				</template>

				<u-field :disabled="!edit" v-model="user.address" label="主体地址" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-field :disabled="true" @click="showNatureBusinessHandler" :right-icon="edit?'arrow-down-fill':''"
					v-model="user.natureBusinessText" placeholde="请选择经营范围" label="经营范围" input-align="right"
					:field-style="edit?placeholderStyle:{}">
				</u-field>
				<u-picker @confirm="selectNatureBusiness" mode="selector" v-model="showNatureBusiness"
					:default-selector="[0]" :range="natureBusinessList" range-key="text"></u-picker>

				<!-- <u-field :disabled="!edit" v-model="user.buyScopeText" placeholder="请输入产品名称" label="收购范围" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field> -->
				<u-field v-if="!edit" :disabled="true" v-model="user.buyScopeText" label="收购范围" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>
				<u-form-item v-else style="padding-right: 15px; padding-left: 10px;" label-width="180" label="收购范围"
					prop="productInformId">
					<u-input v-model="valueProductName || user.buyScopeText" placeholder="请选择收购范围" :type="type"
						input-align="right" :border="false" @click="showNatureHand" />
					<u-picker @confirm="selectNature" mode="selector" v-model="showNature" :default-selector="[0]"
						:range="natureList" range-key="text"></u-picker>
				</u-form-item>



				<u-field :disabled="!edit" v-if="!edit" v-model="user.createTime" label="注册时间" input-align="right"
					:field-style="edit?placeholderStyle:{}"></u-field>

			</view>
			<ferrButton :title="edit?'保存':'编辑信息'" @click="editAndSave"></ferrButton>
		</scroll-view>

	</view>
</template>

<script>
	import freeListItem from "@/components/free-ui/free-list-item.vue";
	import freeAvater from "@/components/free-ui/free-avater.vue";
	import freeDivider from "@/components/free-ui/free-divider.vue";
	import ferrButton from "@/components/free-ui/ferr-button.vue";
	import $H from '@/common/free-lib/request.js';
	import $U from "@/common/free-lib/util.js";

	export default {
		components: {
			freeListItem,
			freeAvater,
			freeDivider,
			ferrButton,
		},
		data() {
			return {
				edit: false,
				showNatureBusiness: false,
				businessStartTermShow: false,
				businessEndTermShow: false,
				foundedTimeShow: false,
				user: {
					abnormal: '',
					address: '',
					agricultureLabel: '',
					avatar: '',
					businessEndTerm: '',
					businessLicense: '',
					businessStartTerm: '',
					businessTerm: '',
					buyScope: '',
					createTime: '',
					enterpriseName: '',
					followUserId: '',
					foundedTime: '',
					id: '',
					isFollow: '',
					legalRepresentative: '',
					natureBusiness: '',
					natureBusinessText: "",
					phone: '',
					registeredCapital: '',
					serviceCode: '',
					state: '',
					token: '',
					type: '',
					unifiedCreditCode: '',
					userType: '',
					username: '',
					fullAvatar: '',
					buyScopeText:''
				},
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				natureBusinessMap: {
					// 经营范围 1科技类 2文化、传媒广告类 3咨询类 4管理类 5商贸类
					"1": "科技类",
					"2": "文化、传媒广告类",
					"3": "咨询类",
					"4": "管理类",
					"5": "商贸类"
				},
				natureBusinessList: [{
						"text": "科技类",
						"value": "1"
					},
					{
						"text": "文化、传媒广告类",
						"value": "2"
					},
					{
						"text": "咨询类",
						"value": "3"
					},
					{
						"text": "管理类",
						"value": "4"
					},
					{
						"text": "商贸类",
						"value": "5"
					}
				],
				mobile: '测试奢侈',
				placeholderStyle: {
					color: '#999'
				},
				type: 'select',
				
				showNature: false,
				natureList: [],
			}
		},
		computed: {

		},
		onShow() {
			this.getUserInfo()
		},
		onLoad() {
			this.getUserInfo();
			this.NewListD()
		},
		methods: {
			handleImageError() {
				uni.showToast({
					title: '图片格式不支持jpeg，请重新上传',
					icon: 'none',
					duration: 4000
				});

			},
			showNatureBusinessHandler() {
				if (!this.edit) {
					return;
				}
				this.showNatureBusiness = true;
			},
			showBusinessStartTermHandler() {
				if (!this.edit) {
					return;
				}
				this.businessStartTermShow = true;
			},
			showBusinessEndTermHandler() {
				if (!this.edit) {
					return;
				}
				this.businessEndTermShow = true;
			},
			showFoundedTimeHandler() {
				if (!this.edit) {
					return;
				}
				this.foundedTimeShow = true;
			},
			selectNatureBusiness(e) {
				this.user.natureBusinessText = this.natureBusinessList[e[0]].text;
				this.user.natureBusiness = this.natureBusinessList[e[0]].value;
			},
			selectBusinessStartTerm(e) {
				this.user.businessStartTerm = e['year'] + '-' + e['month'] + '-' + e['day'];
			},
			selectBusinessEndTerm(e) {
				this.user.businessEndTerm = e['year'] + '-' + e['month'] + '-' + e['day'];
			},
			selectFoundedTimeTerm(e) {
				this.user.foundedTime = e['year'] + '-' + e['month'] + '-' + e['day'];
			},

			// 收购范围/agriculture/productInform/productNameList
			NewListD() {
				$H.get('/basisInform/productName/productNameList', {

				}).then((res) => {
					this.natureList = (res.result || []).map((v) => {
						return {
							text: v.productName,
							id: v.id,
						}
					})
					const target = this.natureList.find(
						(v) => v.id === this.user.id
					)
					this.valueProductName = target ? target.text : ''
				})
			},
			showNatureHand() {
				this.showNature = true;
			},
			selectNature(e) {
				this.valueProductName = this.natureList[e[0]].text
				this.user.buyScope = this.natureList[e[0]].id;

			},
			// uploadBusinessLicense() {
			// 	if(!this.edit){
			// 		return;
			// 	}
			// 	const that = this;

			// 	uni.chooseImage({
			// 		count: 1,
			// 		sizeType: ['compressed'],
			// 		success: (res) => {
			// 			uni.showLoading({
			// 				title: '加载中'
			// 			});
			// 			// 上传
			// 			$H.upload("/sys/common/businessLicense/upload", {
			// 				filePath: res.tempFiles[0].path,
			// 				name: 'file',
			// 				formData: {
			// 					"biz": "temp"
			// 				}
			// 			}).then(res => {
			// 				uni.hideLoading();
			// 				const path = res.message;
			// 				this.user.businessLicense = path;
			// 				// 上传成功后，将图片地址存储在 this.ifdUser.avatar 中
			// 				if (res.result || []) {
			// 					const result = res.result;
			// 					this.user.enterpriseName = result.name;
			// 					this.user.unifiedCreditCode = result.registration_number;
			// 					this.user.registeredCapital = result.registered_capital;
			// 					this.user.legalRepresentative = result.legal_representative;
			// 					this.user.foundedTime = result.found_date.replace(/[^\d]/g, '-').slice(0, -1);
			// 					this.user.address = result.address;
			// 					// 其他字段依次类推
			// 				}
			// 			   this.user.fullAvatar = $H.getFullImageUrl(path);
			// 			}).catch(error => {
			// 				uni.hideLoading();
			// 				console.log(error)
			// 			});
			// 		}
			// 	})
			// },
			uploadBusinessLicense() {
				if (!this.edit) {
					return;
				}
				const that = this;

				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					success: (res) => {
						uni.showLoading({
							title: '加载中'
						});
						// 上传
						$H.uploadLicense("/sys/common/businessLicense/upload", {
							filePath: res.tempFiles[0].path,
							name: 'file',
							formData: {
								"biz": "temp"
							}
						}).then(res => {
							uni.hideLoading();
							const path = res.message;
							if (res.result) {
								// 上传成功后，将图片地址存储在 this.user.businessLicense 中
								this.user.businessLicense = path;
								const result = res.result;
								this.user.enterpriseName = result.name;
								this.user.unifiedCreditCode = result.registration_number;
								this.user.registeredCapital = result.registered_capital;
								this.user.legalRepresentative = result.legal_representative;
								this.user.foundedTime = result.found_date.replace(/[^\d]/g, '-').slice(
									0, -1);
								this.user.address = result.address;
								// 其他字段依次类推
								this.user.fullAvatar = $H.getFullImageUrl(path);
							} else {
								uni.showToast({
									title: '请上传正确的营业执照',
									icon: 'none',
									duration: 4000
								});
								// 清空无效的图片路径
								this.user.businessLicense = null;
							}
						}).catch(error => {
							uni.hideLoading();
							console.log(error);
							uni.showToast({
								title: '上传失败',
								icon: 'none',
								duration: 4000
							});
						});
					}
				});
			},
			openCode() {
				uni.navigateTo({
					url: '/pages/my/code/code'
				})
			},
			fullTime(time) {
				if (!time) {
					return "";
				}
				return time + " 00:00:00";
			},
			editAndSave() {
				if (!this.edit) {
					this.edit = true;
					return;
				}
				const that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认修改',
					success: function(res) {
						if (res.confirm) {
							//修改用戶信息
							uni.showLoading({
								title: '加载中'
							});
							$H.post("/brand/user/upper/edit", {
								id: that.user.id,
								address: that.user.address,
								businessEndTerm: that.fullTime(that.user.businessEndTerm),
								businessLicense: that.user.businessLicense,
								businessStartTerm: that.fullTime(that.user.businessStartTerm),
								buyScope: that.user.buyScope,
								enterpriseName: that.user.enterpriseName,
								foundedTime: that.fullTime(that.user.foundedTime),
								legalRepresentative: that.user.legalRepresentative,
								natureBusiness: that.user.natureBusiness,
								phone: that.user.phone,
								registeredCapital: that.user.registeredCapital,
								unifiedCreditCode: that.user.unifiedCreditCode,
								userType: that.user.userType,
								username: that.user.username
							}, {}).then(res => {
								uni.hideLoading();
								if (res.success) {
									uni.showToast({
										title: res.message,
										icon: "none",
									});
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										});
									}, 1000);
								} else {
									uni.showToast({
										title: res.message,
										icon: "none",
									});
								}
							}).catch(error => {
								console.error('请求出错', error); // 处理任何错误
								uni.showToast({
									title: "修改错误",
									icon: "none",
								});
								uni.hideLoading();
							})
						}
					}
				});
			},
			getUserInfo() {
				uni.showLoading({
					title: '加载中'
				});
				const userId = $U.getStorage("id");

				if (!userId) {
					uni.showToast({
						title: "请重新登录",
						icon: "error",
					});
				}

				$H.get(
					"/brand/user/queryById", {
						id: userId,
					}, {}
				).then((res) => {
					if (!res.success) {
						uni.showToast({
							title: "未查询到相关信息",
							icon: "none",
						});
						return;
					}
					this.user.abnormal = res.result.abnormal;
					this.user.address = res.result.address;
					this.user.agricultureLabel = res.result.agricultureLabel;
					this.user.avatar = res.result.avatar;
					this.user.fullAvatar = $H.getFullImageUrl(res.result.businessLicense);
					this.user.businessEndTerm = res.result.businessEndTerm;
					this.user.businessLicense = res.result.businessLicense;
					this.user.businessStartTerm = res.result.businessStartTerm;
					this.user.businessTerm = res.result.businessTerm;
					this.user.buyScope = res.result.buyScope;
					this.user.createTime = res.result.createTime;
					this.user.enterpriseName = res.result.enterpriseName;
					this.user.followUserId = res.result.followUserId;
					this.user.foundedTime = res.result.foundedTime;
					this.user.id = res.result.id;
					this.user.isFollow = res.result.isFollow;
					this.user.legalRepresentative = res.result.legalRepresentative;
					this.user.natureBusiness = res.result.natureBusiness;
					this.user.natureBusinessText = this.natureBusinessMap[res.result.natureBusiness];
					this.user.phone = res.result.phone;
					this.user.registeredCapital = res.result.registeredCapital;
					this.user.serviceCode = res.result.serviceCode;
					this.user.state = res.result.state;
					this.user.token = res.result.token;
					this.user.type = res.result.type;
					this.user.unifiedCreditCode = res.result.unifiedCreditCode;
					this.user.userType = res.result.userType;
					this.user.username = res.result.username;
					this.user.buyScopeText = res.result.buyScopeText;
					uni.hideLoading();
				}).catch(error => {
					console.error('请求出错', error); // 处理任何错误
					uni.showToast({
						title: "查询错误",
						icon: "none",
					});
					uni.hideLoading();
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-border-bottom:after {
		border-bottom: 2px solid #dee2e6 !important;

	}
</style>